package com;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.multidex.MultiDex;

import com.yiguantong.police.ui.utils.AESUtil;
import com.yiguantong.police.ui.utils.SharedPreferencesUtil;

public class MainApplication extends Application {
    Toast toast;
    //IMEI
    public static String IMEI = "";
    //IMSI
    public static String IMSI = "";
    private static PackageInfo packInfo;
    /**
     * 单例对象
     */
    private static MainApplication instance = null;

    public static MainApplication getIns() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (null == instance) {
            instance = this;
        }
    }

    /**
     * Toast展示
     *
     * @param str 字符串
     */
    public void tip(String str) {
        if (toast == null) {
            toast = Toast.makeText(MainApplication.getIns(), str, Toast.LENGTH_SHORT);
        } else {
            toast.setText(str);
        }
        toast.show();
    }

    /**
     * 屏幕宽度 方法名称：getScreenWidth
     */
    public int getScreenWidth() {
        return MainApplication.getIns().getResources().getDisplayMetrics().widthPixels;
    }

    public int getScreenHeight() {
        return MainApplication.getIns().getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 获取屏幕密度
     * <p/>
     * 方法名称：getDensity
     */
    public float getDensity() {
        return MainApplication.getIns().getResources().getDisplayMetrics().density;
    }

    /**
     * 获取存储手机号
     *
     * @return
     */
    public String getUserMobile() {
        String userMobile = SharedPreferencesUtil.getString(this, "loginMobile", "");
        if (!TextUtils.isEmpty(userMobile)) {
            userMobile = AESUtil.loaclDecode(userMobile);
        }
        if (!TextUtils.isEmpty(userMobile)) {
            return userMobile;
        } else {
            return null;
        }
    }

    /**
     * 获取存储AES密钥
     *
     * @return
     */
    public String getSecretKey() {
        String secretKey = SharedPreferencesUtil.getString(this, "secretKey", "");
        /*if (!TextUtils.isEmpty(secretKey)){
            secretKey = AESUtil.loaclDecode(secretKey);
        }*/
        if (!TextUtils.isEmpty(secretKey)) {
            return secretKey;
        } else {
            return null;
        }
    }

    /**
     * 方法名称：getVersionName
     * <p/>
     * 方法描述：获取系统的版本名称 versionName
     *
     * @return versionName 4.2
     * <p/>
     * 备注：
     * @author wangZhongfu
     */
    public String getVersionName() {
        if (packInfo == null) {
            getAppInfo();
        }
        return packInfo != null ? packInfo.versionName : "";
    }

    private void getAppInfo() {
        // 获取packageManager的实例
        PackageManager packageManager = getPackageManager();
        try {
            packInfo = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
